<template>
  <div class="manage">
    <el-card>
      <el-form inline :model="pageParams">
        <el-form-item label="一体杆名称">
          <el-input size="small" placeholder="请输入一体杆名称" v-model="pageParams.poleName"/>
        </el-form-item>
        <el-form-item label="一体杆编号">
          <el-input size="small" placeholder="请输入一体杆编号" v-model="pageParams.poleNumber"/>
        </el-form-item>
        <el-form-item label="运行状态">
          <el-select size="small" v-model="pageParams.poleStatus" placeholder="请选择一体杆运行状态">
            <el-option v-for="(item) in poleStatusArray" :value="item.value" :label="item.label" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-search" type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" size="small" icon="el-icon-plus" @click="handleAdd">添加一体杆</el-button>
      <el-button type="danger" size="small" icon="el-icon-delete" @click="handleBatchDelete">批量删除</el-button>
      <el-table border size="small" stripe :header-cell-style="{ backgroundColor: '#f4f6f8' }" :data="poleList" v-loading="loading" element-loading-text="拼命加载中" @selection-change="handleSelectionChange">
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column label="一体杆名称" prop="poleName" align="center"></el-table-column>
        <el-table-column label="一体杆编号" prop="poleNumber" align="center"></el-table-column>
        <el-table-column label="一体杆IP" prop="poleIp" align="center"></el-table-column>
        <el-table-column label="一体杆类型" :formatter="formatterPoleType" align="center"></el-table-column>
        <el-table-column label="安装区域" prop="poleName" align="center"></el-table-column>
        <el-table-column label="运行状态" :formatter="formatterPoleStatus" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="warning"  size="small" icon="el-icon-edit" @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-plus" size="small" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[15, 20, 30, 40]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background></el-pagination>
    </el-card>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="35%" @close="$refs.poleForm.resetForm()">
      <pole-form :edit="edit" ref="poleForm" @success="handleSuccess"></pole-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { poleStatusArray } from '@/constants'
import PoleForm from '@/views/rod/manage/components/poleForm/poleForm.vue'

export default {
  name: 'manage',
  components: { PoleForm },
  data() {
    return {
      /* 获取一体杠列表的分页参数 */
      pageParams: {
        page: 1,
        pageSize: 15,
        poleName: undefined,
        poleNumber: undefined,
        poleStatus: null
      },
      /* 待批量删除的id数组 */
      poleIds: [],
      /* 是否处于加载列表状态 */
      loading: false,
      /* 运行状态数组 */
      poleStatusArray,
      /* 是否显示弹框组件 */
      dialogVisible: false,
      edit: false
    }
  },
  computed: {
    ...mapGetters('manageStore', ['poleList', 'total']),
    title() {
      return this.edit ? '编辑一体杠' : '添加一体杠'
    }
  },
  methods: {
    /* 获取一体杠列表的回调函数 */
    async fetchPoleList() {
      try {
        this.loading = true
        await this.$store.dispatch('manageStore/fetchPoleList', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /* 删除一体杠的回调函数 */
    async deletePole(id) {
      try {
        const deleteResult = await this.$store.dispatch('manageStore/deletePole', id)
        await this.fetchPoleList()
        this.$message.success(deleteResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    formatterPoleStatus(row, column, cellValue, index) {
      const MAP = { 0: '正常', 1: '异常' }
      return MAP[row.poleStatus] || '未知'
    },
    formatterPoleType(row, column, cellValue, index) {
      const MAP = { 'entrance': '出口', 'export': '入口' }
      return MAP[row.poleType] || '未知'
    },
    /* 点击搜索按钮的回调函数 */
    handleSearch() {
      this.pageParams.page = 1
      this.fetchPoleList()
    },
    /* 分页器size变化的回调函数 */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchPoleList()
    },
    /* 分页器page变化的回调函数 */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchPoleList()
    },
    /* 表格组件的选中单选框事件 */
    handleSelectionChange(poleList) {
      this.poleIds = poleList.map(item => item.id)
    },
    /* 点击删除按钮的回调函数 */
    async handleDelete(row) {
      this.deletePole(row.id)
    },
    /* 点击批量删除按钮的回调函数 */
    handleBatchDelete() {
      if (this.poleIds.length <= 0) return this.$message.warning('请选择需要批量删除的一体杠')
      this.deletePole(this.poleIds.join(','))
    },
    /* 点击添加按钮的回调函数 */
    handleAdd() {
      this.edit = false
      this.dialogVisible = true
    },
    /* 点击编辑按钮的回调函数 */
    handleEdit(row) {
      this.edit = true
      this.dialogVisible = true
      this.$nextTick(()=>{
        this.$refs.poleForm.findPole(row)
      })
    },
    handleSuccess() {
      this.dialogVisible = false
      this.fetchPoleList()
    }
  },
  mounted() {
    this.fetchPoleList()
  }
}
</script>

<style scoped lang="scss">
.manage {
  padding: 10px 0;
  .el-card {
    margin-bottom: 10px;

    .el-table {
      margin: 10px 0;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }

  }

}
</style>

